﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLibrary.BLL;
using CommonLibrary.Model;
using SmsSeries.ServiceSmsSeries;

namespace SmsSeries
{
    public partial class frmSendMsg : Form
    {
        #region 变量

        SmsSeriesSoapClient smsSeriesSoapClient = new SmsSeriesSoapClient();

        List<RadioButton> rbPrioritys = new List<RadioButton>();

        /// <summary>
        /// TabPage
        /// </summary>
        private enum TabPageEnum
        {
            /// <summary>
            /// 发送
            /// </summary>
            Send,
            /// <summary>
            /// 联系人
            /// </summary>
            Linkman,
            /// <summary>
            /// 联系人分组
            /// </summary>
            LinkmanGroup
        }

        /// <summary>
        /// 是否是更新
        /// </summary>
        private bool isUpdate;

        #endregion

        #region 属性

        /// <summary>
        /// 使能菜单
        /// </summary>
        private bool EnableToolBar
        {
            set
            {
                tsmiDeleteTreeNode.Enabled = value;
                tsmiUpdateTreeNode.Enabled = value;
                tsmiAddLinkmanTreeNode.Enabled = value;
            }
        }

        #endregion

        #region 控件事件

        //构造函数
        public frmSendMsg()
        {
            InitializeComponent();
        }

        //发送
        private void btnSend_Click(object sender, EventArgs e)
        {
            int priority = 0;
            for (int i = 0; i < rbPrioritys.Count; i++)
            {
                if (rbPrioritys[i].Checked)
                {
                    priority = Convert.ToInt32(rbPrioritys[i].Tag);
                }
            }

            UserInfoInfo userInfoInfo = (UserInfoInfo) cbUserID.SelectedItem;

            smsSeriesSoapClient.SendMsg(userInfoInfo.UserName.ToString(), tbDestNumber.Text,
                tbContent.Text, tbSignName.Text, priority, (int)nuValidMinute.Value);

            Close();
        }

        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        //窗体加载
        private void frmSendMsg_Load(object sender, EventArgs e)
        {
            rbPrioritys.Add(rbPriority1);
            rbPrioritys.Add(rbPriority2);
            rbPrioritys.Add(rbPriority3);

            SelectTabPage(TabPageEnum.Send);

            LoadTreeNodes();

            EnableToolBar = false;

            //加载UserID
            List<UserInfoInfo> userInfoInfos = UserInfo.GetList("");
            foreach (UserInfoInfo userInfoInfo in userInfoInfos)
            {
                cbUserID.Items.Add(userInfoInfo);
            }
            if (cbUserID.Items.Count > 0)
            {
                cbUserID.SelectedIndex = 0;
            }
        }

        //TreeNodes 上单击鼠标左键
        private void tvLinkman_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                tvLinkman.SelectedNode = e.Node;
                EnableToolBar = true;
            }
        }

        //TreeNodes 上双击鼠标
        private void tvLinkman_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if(e.Node.Parent == null)
            {
                //组
                foreach (TreeNode node in e.Node.Nodes)
                {
                    //联系人
                    char[] spliter = { '-' };
                    string[] strings = node.Name.Split(spliter);
                    List<LinkmanInfo> linkmanInfos = Linkman.GetList(string.Format("LinkmanID = {0}", strings[1]));
                    if (tbDestNumber.Text == "")
                    {
                        tbDestNumber.Text = linkmanInfos[0].LinkmanTel;
                    }
                    else
                    {
                        tbDestNumber.AppendText(string.Format(";{0}", linkmanInfos[0].LinkmanTel));
                    }
                }
            }
            else
            {
                //联系人
                char[] spliter = {'-'};
                string[] strings = e.Node.Name.Split(spliter);
                List<LinkmanInfo> linkmanInfos = Linkman.GetList(string.Format("LinkmanID = {0}", strings[1]));
                if (tbDestNumber.Text == "")
                {
                    tbDestNumber.Text = linkmanInfos[0].LinkmanTel;
                }
                else
                {
                    tbDestNumber.AppendText(string.Format(";{0}", linkmanInfos[0].LinkmanTel));
                }
            }
        }

        //TreeNode 菜单关闭前
        private void cmsTreeNodes_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            EnableToolBar = false;
        }

        //菜单 添加联系人
        private void tsmiAddLinkmanTreeNode_Click(object sender, EventArgs e)
        {
            SelectTabPage(TabPageEnum.Linkman);
            btnOKLinkman.Text = "添加";
            isUpdate = false;

            tbLinkmanName.Text = "";
            tbLinkmanTel.Text = "";
        }

        //菜单添加联系人分组
        private void tsmiAddGroupTreeNode_Click(object sender, EventArgs e)
        {
            SelectTabPage(TabPageEnum.LinkmanGroup);

            btnOKGroup.Text = "添加";
            isUpdate = false;

            tbGroupName.Text = "";
        }

        //菜单 更新
        private void tsmiUpdateTreeNode_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = tvLinkman.SelectedNode;

            if (selectedNode.Parent == null)
            {
                SelectTabPage(TabPageEnum.LinkmanGroup);

                btnOKGroup.Text = "修改";
                isUpdate = true;

                tbGroupName.Text = selectedNode.Text;
            }
            else
            {
                SelectTabPage(TabPageEnum.Linkman);
                btnOKLinkman.Text = "修改";
                isUpdate = true;

                LinkmanInfo linkmanInfo = (LinkmanInfo) selectedNode.Tag;
                tbLinkmanName.Text = linkmanInfo.LinkmanName;
                tbLinkmanTel.Text = linkmanInfo.LinkmanTel;
            }
        }

        //菜单 删除
        private void tsmiDeleteTreeNode_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = tvLinkman.SelectedNode;

            if (selectedNode.Parent == null)
            {
                if (MessageBox.Show(string.Format("请确认是否要删除联系人组‘{0}’？",selectedNode.Text), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //删除联系人
                    foreach (TreeNode node in selectedNode.Nodes)
                    {
                        char[] spliter1 = { '-' };
                        string[] strings1 = node.Name.Split(spliter1);

                        Linkman.Delete(Convert.ToInt32(strings1[1]));
                    }

                    //删除联系人组
                    Linkman_Group.Delete(Convert.ToInt32(selectedNode.Name));

                    //删除关联
                    LinkmanGroup.DeleteByLinkmanGroupNo(Convert.ToInt32(selectedNode.Name));

                    tvLinkman.Nodes.Remove(tvLinkman.SelectedNode);
                }
            }
            else
            {
                if (MessageBox.Show(string.Format("请确认是否要删除联系人‘{0}’？",selectedNode.Text), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //删除联系人
                    char[] spliter = { '-'};
                    string[] strings = selectedNode.Name.Split(spliter);

                    Linkman.Delete(Convert.ToInt32(strings[1]));

                    //删除关联
                    LinkmanGroup.DeleteByLinkmanID(Convert.ToInt32(strings[1]));

                    tvLinkman.Nodes.Remove(tvLinkman.SelectedNode);
                }
            }
        }

        //联系人 添加修改
        private void btnOKLinkman_Click(object sender, EventArgs e)
        {
            if (!isUpdate)
            {
                LinkmanInfo linkmanInfo=new LinkmanInfo(tbLinkmanName.Text,tbLinkmanTel.Text);
                Linkman.Insert(linkmanInfo);

                //取到最大的
                List<LinkmanInfo> linkmanInfos = Linkman.GetList("Linkmanid = (select max(Linkmanid) from Linkman)");

                //关联
                LinkmanGroupInfo linkmanGroupInfo = new LinkmanGroupInfo(Convert.ToInt32(
                    tvLinkman.SelectedNode.Parent ==null ? tvLinkman.SelectedNode.Name : tvLinkman.SelectedNode.Parent.Name
                    ), linkmanInfos[0].LinkmanID);
                LinkmanGroup.Insert(linkmanGroupInfo);

                if (tvLinkman.SelectedNode.Parent == null)
                {
                    TreeNode node=new TreeNode();
                    node.Name = string.Format("{0}-{1}", tvLinkman.SelectedNode.Name,
                                              linkmanInfos[0].LinkmanID.ToString());
                    node.Text = linkmanInfos[0].LinkmanName;
                    node.Tag = linkmanInfos[0];
                    tvLinkman.SelectedNode.Nodes.Add(node);
                    
                }
                else
                {
                    TreeNode node=new TreeNode();
                    node.Name = string.Format("{0}-{1}", tvLinkman.SelectedNode.Parent.Name,
                                              linkmanInfos[0].LinkmanID.ToString());
                    node.Text = linkmanInfos[0].LinkmanName;
                    node.Tag = linkmanInfos[0];
                    tvLinkman.SelectedNode.Parent.Nodes.Add(node);
                }
            }
            else
            {    
                char[] spliter = { '-' };
                string[] strings =   tvLinkman.SelectedNode.Name.Split(spliter);
                LinkmanInfo linkmanInfo = new LinkmanInfo(Convert.ToInt32(strings[1]), tbLinkmanName.Text, tbLinkmanTel.Text);
                Linkman.Update(linkmanInfo);

                tvLinkman.SelectedNode.Text = linkmanInfo.LinkmanName;
                tvLinkman.SelectedNode.Tag = linkmanInfo;
            }
        }

        //联系人 取消
        private void btnCancelLinkman_Click(object sender, EventArgs e)
        {
            SelectTabPage(TabPageEnum.Send);
        }

        //联系人分组 添加修改
        private void btnOKGroup_Click(object sender, EventArgs e)
        {
            if(!isUpdate)
            {
                Linkman_GroupInfo linkman_GroupInfo = new Linkman_GroupInfo(tbGroupName.Text);
                Linkman_Group.Insert(linkman_GroupInfo);
                tbGroupName.Text = "";
                List<Linkman_GroupInfo> linkman_GroupInfos =
                    Linkman_Group.GetList("linkmanGroupNo = (select max(linkmanGroupNo) from linkman_Group)");
                TreeNode node=new TreeNode();
                node.Name = linkman_GroupInfos[0].LinkmanGroupNo.ToString();
                node.Text = linkman_GroupInfos[0].LinkmanGroupName;
                node.Tag = linkman_GroupInfos[0];
                tvLinkman.Nodes.Add(node);
            }
            else
            {
                Linkman_GroupInfo linkman_GroupInfo = new Linkman_GroupInfo(
                    Convert.ToInt32(tvLinkman.SelectedNode.Name), tbGroupName.Text);
                Linkman_Group.Update(linkman_GroupInfo);
                tbGroupName.Text = "";

                tvLinkman.SelectedNode.Text = linkman_GroupInfo.LinkmanGroupName;
                tvLinkman.SelectedNode.Tag = linkman_GroupInfo;
            }
        }

        //联系人分组 取消
        private void btnCancelGroup_Click(object sender, EventArgs e)
        {
            SelectTabPage(TabPageEnum.Send);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 选择TabPage
        /// </summary>
        /// <param name="tabPageEnum">tabPage</param>
        private void SelectTabPage(TabPageEnum tabPageEnum)
        {
            switch (tabPageEnum)
            {
                case TabPageEnum.Send:

                    pSend.Visible = true;
                    pLinkman.Visible = false;
                    pGroup.Visible = false;
                    pSend.Dock = DockStyle.Fill;

                    break;
                case TabPageEnum.Linkman:

                    pSend.Visible = false;
                    pLinkman.Visible = true;
                    pGroup.Visible = false;
                    pLinkman.Dock = DockStyle.Fill;

                    break;
                case TabPageEnum.LinkmanGroup:

                    pSend.Visible = false;
                    pLinkman.Visible = false;
                    pGroup.Visible = true;
                    pGroup.Dock = DockStyle.Fill;

                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 加载树
        /// </summary>
        private void LoadTreeNodes()
        {
            tvLinkman.Nodes.Clear();

            List<Linkman_GroupInfo> linkman_GroupInfos = Linkman_Group.GetList("");

            if (linkman_GroupInfos.Count > 0)
            {
                foreach (Linkman_GroupInfo linkman_GroupInfo in linkman_GroupInfos)
                {
                    TreeNode node = new TreeNode();
                    node.Name = linkman_GroupInfo.LinkmanGroupNo.ToString();
                    node.Text = linkman_GroupInfo.LinkmanGroupName;
                    node.Tag = linkman_GroupInfo;
                    tvLinkman.Nodes.Add(node);

                    List<LinkmanGroupInfo> linkmanGroupInfos = LinkmanGroup.GetList(string.Format(" LinkmanGroupNO = {0}",node.Name) );
                    foreach (LinkmanGroupInfo linkmanGroupInfo in linkmanGroupInfos)
                    {
                        List<LinkmanInfo> linkmanInfos =
                            Linkman.GetList(string.Format(" LinkmanID = {0}", linkmanGroupInfo.LinkmanID));
                        if(linkmanInfos.Count > 0)
                        {
                            TreeNode node1 = new TreeNode();
                            node1.Name = string.Format("{0}-{1}", linkman_GroupInfo.LinkmanGroupNo, linkmanInfos[0].LinkmanID);
                            node1.Text = linkmanInfos[0].LinkmanName;
                            node1.Tag = linkmanInfos[0];
                            node.Nodes.Add(node1);
                        }
                    }
                }
                tvLinkman.ExpandAll();
            }
        }

        #endregion
    }
}
